package com.mes.handler.report;

import java.sql.Connection;
import java.util.HashMap;
import java.util.Map;
import net.sf.jasperreports.engine.JasperRunManager;
import org.apache.log4j.Logger;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import org.openbravo.client.kernel.BaseActionHandler;
import org.openbravo.dal.service.OBDal;

@SuppressWarnings("unchecked")
public class JasperReportHandler extends BaseActionHandler {
	private Logger logger = Logger.getLogger(this.getClass());

	@Override
	protected JSONObject execute(Map<String, Object> parameters, String data) {
		JSONObject jsonRep = new JSONObject();
		Connection conn = null;
		try {
			JSONObject jsonData = new JSONObject(data);
			String resourcePath = "";
			conn = OBDal.getInstance().getConnection();
			Map params = new HashMap();
			JasperRunManager.runReportToHtmlFile(resourcePath, params, conn);
			
//			File reportFile = new File(resourcePath);
//			JasperReport jasperReport = (JasperReport) JRLoader.loadObject(reportFile);
//			JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, conn);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("数据链查询失败：", e);
			try {
				jsonRep.put("success", false);
				jsonRep.put("msg", "查询失败：" + e.getMessage());
			} catch (JSONException e1) {
			}
		} finally {
			if (conn != null) {
				try {
					conn.close();
				} catch (Exception e) {
				}
			}
		}
		return jsonRep;
	}
}
